from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import os
from selenium.webdriver.chrome.options import Options

username = ""
password = ""
driver_path = r"C:\Program Files\Google\Chrome\Application\chromedriver.exe"
targetUrl="https://yqfk.dgut.edu.cn/main"
loginUrl = "https://cas.dgut.edu.cn/home/Oauth/getToken/appid/illnessProtectionHome/state/home.html"






if __name__ == '__main__':
    driver = webdriver.Chrome(executable_path=driver_path)
    driver.get(loginUrl)

    # 隐式等待
    driver.implicitly_wait(10)
    time.sleep(2)
    if driver.current_url == loginUrl:  ## 未登录
        driver.find_element_by_id("username").send_keys(username)
        driver.find_element_by_id("casPassword").send_keys(password)
        driver.save_screenshot("login.png")
        driver.find_element_by_id("loginBtn").click()
        driver.implicitly_wait(10)

    result = WebDriverWait(driver, 10).until(
        EC.url_to_be("https://yqfk.dgut.edu.cn/main")
    )

    if result:
        time.sleep(2)
        driver.save_screenshot("before.png")
        driver.find_element_by_xpath('//*[@id="root"]/div/div/form/div/div[29]/a').click()
        time.sleep(2)
        driver.save_screenshot("after.png")

    driver.close()